// pages/voucher/voucher.js
const $api = require('../../utils/request').Api;
const login = wx.getStorageSync('login') // login.data.data.memberNumber
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		activeNames: ['1'],
		isFold: false,
		leimingqu: '',
		listdata: [],
    totalPrice: 0,
    baseImgUrl: app.globalData.baseImgUrl,
	},
	onSelectVoucher (e) {
		const { data, id } = e.currentTarget.dataset
		const eventChannel = this.getOpenerEventChannel()
		eventChannel.emit('selected', { ...data, id });
		wx.navigateBack()
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.data.totalPrice = +options.price
		this.getListUserVoucher()
	},
	handleTrigger(e) {
		this.isFold = !this.isFold;
		// if(e.currentTarget.dataset.item){}
		if (this.isFold === true) {
			this.leimingqu = e.currentTarget.dataset.item ? e.currentTarget.dataset.item : '0'
		} else {
			if (this.leimingqu != '') {
				console.log(e.currentTarget.dataset.item.index == this.leimingqu.index);
				this.leimingqu = e.currentTarget.dataset.item.index == this.leimingqu.index ? '0' : e.currentTarget.dataset.item
			} else {

			}
			// this.leimingqu = e.currentTarget.dataset.item ? '0' : '0'
		}
		console.log(this.leimingqu);
		this.setData({
			isFold: this.isFold,
			leimingqu: this.leimingqu
		})
	},
	onChange(event) {
		this.setData({
			activeNames: event.detail,
		});
	},
  getListUserVoucher () {
    $api.getListUserVoucher({ memberNumber: login.data.data.memberNumber, pageNum: 1, pageSize: 10 }).then(({ data: res }) => {
      this.setData({ 
        listdata: res.data.list.filter(item => item.voucher.actualAmount <= this.data.totalPrice && item.status === 0 ).map((item, index) => ({ ...item, index: index + 1 + '' })) 
      })
    })
  }
  
})
